package com.yangrui.app.service.impl;

import com.yangrui.app.entity.VideoBanner;
import com.yangrui.app.mapper.VideoBannerMapper;
import com.yangrui.app.service.VideoBannerService;
import com.yangrui.app.util.GuavaTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Service
public class VideoBannerServiceImpl implements VideoBannerService {

    @Autowired
    private GuavaTemplate guavaTemplate;
    @Autowired
    private VideoBannerMapper videoBannerMapper;

    @Override
    public List<VideoBanner> list() {
        Object cache = null;
        try {
            cache = guavaTemplate.getTenMinuteCache().get("banner",()->{
                System.out.println("从数据库获取banner数据");
                return videoBannerMapper.list();
            });
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        if(cache instanceof  List){
            return (List<VideoBanner>) cache;
        }
        return new ArrayList<>();
    }
}

